﻿

using System.Collections;
/// <summary>
/// 处理流程基类
/// </summary>
public class BaseProceduar {

    /// <summary>
    /// 消息
    /// </summary>
    public class Message
    {
        public int msgId;
        public int iParam;
        public object oParam;

        public Message(int mid, int iparam = 0, object oparam = null)
        {
            this.msgId = mid; this.iParam = iparam; this.oParam = oparam;
        }

        public override string ToString()
        {
            return "MessageID - " + msgId + ", IntParam - " + iParam + ", ObjectParam - " + oParam;
        }
    }

    /// <summary>
    /// 消息队列
    /// </summary>
    Queue messages;
	volatile bool _paused;

    public BaseProceduar()
    {
        messages = new Queue();
    }

	/// <summary>
	/// 暂停处理IO消息
	/// </summary>
	/// <value><c>true</c> if paused; otherwise, <c>false</c>.</value>
	public bool paused {
		set { 
			_paused = value;
		}
	}

    /// <summary>
    /// 把消息加入队列
    /// </summary>
    /// <param name="message"></param>
    public void EnqueueMessage(Message message)
    {
        messages.Enqueue(message);
    }

    /// <summary>
    /// 进入流程
    /// </summary>
    public virtual void EnterProceduar(bool initUI)
    {

    }

    /// <summary>
    /// 更新流程
    /// </summary>
    public virtual void UpdateProceduar()
    {
        if (messages.Count > 0)
        {
            Message message = messages.Dequeue() as Message;
            HandleMessage(message);
        }

		if (_paused) {
			return;
		}

        IOMessage ioMessage = NetIOController.inst.DequeueMessage();
        if (ioMessage != null)
        {
            ProcessPacket(ioMessage);
        }
    }

    /// <summary>
    /// 退出流程
    /// </summary>
    public virtual void LeaveProceduar()
    {

    }

    /// <summary>
    /// 处理消息
    /// </summary>
    /// <param name="message"></param>
    protected virtual void HandleMessage(Message message)
    {
        DebugInfo.Warning("未知的消息：" + message.ToString());
    }

    /// <summary>
    /// 处理网络数据包
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    protected virtual bool ProcessPacket(IOMessage message)
    {
        switch (message.code)
        {
            case IOCmds.SMSG_PING:
                // 收到服务器Ping消息
                NetIOController.inst.SendMessage(new IOMessage(IOCmds.CMSG_PONG));
                return true;

            case IOCmds.WS_CONN_OPEN:
                // 与服务器的连接打开
                UIRootController.inst.ShowMask(false);
                return true;

            case IOCmds.WS_CONN_CLOSED:
                // 连接失败或者/连接断开
                UIRootController.inst.ShowMask(true);
                TimerController.inst.CallAfter(1.0f, param => {
                    NetIOController.inst.Connect(null);
                });
                return true;

            case IOCmds.WS_CONN_ERROR:
                return true;

            default:
                DebugInfo.Warning("未知的网络消息：" + message.ToJSON());
                return true;
        }
    }
}
